<template>
	<view class="container" :class="'theme-'+theme">
		<view class="addTitle">
			<view class="titleDetail">
				<view class="" style="flex:1;text-align:left">
					<image src="../../static/newh5/fanhui.png" class="yticon"
						style="width: 13px;height: 20px;color:black" @click="returnIndex()"></image>
				</view>
				<view class="titleText" style="flex:1">
					<b>{{isAddon?'追加评价':'查看评价'}}</b>
				</view>
				<view v-if="!isAddon" class="titleRight" style="color: #269cd3;flex:1"></view>
				<view class="titleRight themeTextColor" v-else @click="publishReview">发布</view>
			</view>
		</view>
		<view class="supplier-section">
			<view class="name-area">
				<image class="logo" src="../../static/store.png" mode="aspectFill" />
				<view class="s-name">{{orderData.supplierName}}</view>
			</view>
			<view class="s-rate" v-for="(item,index) in rateList" :key="index">
				<view class="tit">{{item}}</view>
				<u-rate :current="rates[index]" :disabled="true" class="rate" size="46" active-color="#fdc93e"
					inactive-color="#B6B1B1" :allow-half="true"></u-rate>
			</view>
		</view>
		<view class="goods-section">
			<view class="good-item" v-for="(item,index) in reviewData.items" :key="item.id">
				<view class="good-info">
					<image class="image-wrapper" :src="item.productThumbnail" mode="aspectFit" />
					<view class="item-right">
						<view class="title more-t">{{item.productName}}</view>
						<view class="price">￥{{item.productPrice|money}}</view>
					</view>
				</view>
				<view class="g-rate">
					<view class="tit">商品评分</view>
					<u-rate :current="item.score" :disabled="true" class="rate" size="46" active-color="#fdc93e"
						inactive-color="#B6B1B1" :allow-half="true"></u-rate>
				</view>
				<textarea :disabled="true" v-model="item.content" class="content"></textarea>

				<u-form-item v-if="item.files.length>0" :label-style="labelStyle" label-position="top"
					label-width="150">
					<u-row>
						<u-col :span="4" v-for="(pic,index1) in item.files" :key="index1">
							<u-image border-radius="10" ref="uImage" width="180" height="180" :src="pic.fileUrl"
								mode="aspectFill" @click="doPreview(item.files,0,index1)">
							</u-image>
						</u-col>
					</u-row>

				</u-form-item>
				<template v-if="item.replyList">
					<view v-for="(replyItem,indexReply) in item.replyList" :key="indexReply">
						<u-form-item label-position="top" :label-style="labelStyle" label="卖家回复">
							<u-input type="textarea" :disabled="true" :border="true" v-model="replyItem.content">
							</u-input>
						</u-form-item>
						<u-row v-if="replyItem.replyImgUrl">
							<u-col :span="4" v-for="(fileItem,fileIndex) in replyItem.replyImgUrl.split(',')"
								:key="fileIndex">
								<u-image v-if="fileItem" border-radius="10" ref="uImage" width="180" height="180"
									:src="imgUrl + fileItem" mode="aspectFill"
									@click="doPreview(replyItem.replyImgUrl.split(','),1,fileIndex)">
								</u-image>
							</u-col>
						</u-row>
					</view>
				</template>
				<template v-if="item.addonList">
					<view v-for="addon in item.addonList" :key="addon.id">
						<view style="margin-top: 20rpx;">追加评价：</view>
						<textarea disabled v-model="addon.content" class="content"></textarea>
						<template v-if="addon.replyList">
							<view v-for="(replyItem,indexReply) in addon.replyList" :key="indexReply">
								<u-form-item label-position="top" :label-style="labelStyle" label="卖家回复">
									<u-input type="textarea" :disabled="true" :border="true" v-model="replyItem.content">
									</u-input>
								</u-form-item>
								<u-row v-if="replyItem.replyImgUrl">
									<u-col :span="4" v-for="(fileItem,fileIndex) in replyItem.replyImgUrl.split(',')"
										:key="fileIndex">
										<u-image v-if="fileItem" border-radius="10" ref="uImage" width="180" height="180"
											:src="imgUrl + fileItem" mode="aspectFill"
											@click="doPreview(replyItem.replyImgUrl.split(','),1,fileIndex)">
										</u-image>
									</u-col>
								</u-row>
							</view>
						</template>
					</view>
				</template>
				<template v-if="isAddon">
					<view style="margin-top: 20rpx;">追加评价：</view>
					<textarea :disabled="!isAddon" v-model="item.contexts" placeholder-class="ph"
						:placeholder="isAddon?'请输入追加评价内容':''" class="content"></textarea>
				</template>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		goback
	} from "@/common/utils/navigateBack.js";
	import {
		getBySn,
		addReview,
		seeReview,
		addAddonReviewItem
	} from '../../api/order/order.js'
	// import {imgUrl} from '../../common/utils/config'
	import tuiRate from '../../components/tui-rate.vue'
	import UButton from "../../uview-ui/components/u-button/u-button";
	import URate from "../../uview-ui/components/u-rate/u-rate";
	import UImage from "../../uview-ui/components/u-image/u-image";
	import UFormItem from "../../uview-ui/components/u-form-item/u-form-item";
	import UInput from "../../uview-ui/components/u-input/u-input";
	import UCol from "../../uview-ui/components/u-col/u-col";
	export default {
		components: {
			UCol,
			UInput,
			UFormItem,
			UImage,
			URate,
			UButton,
			tuiRate
		},
		data() {
			return {
				// 是否追评
				isAddon: false,
				imgUrl: '',
				orderData: {},
				rates: [],
				rateList: [
					'服务态度',
					'商品质量',
					'商品价格',
					'物流服务'
				],
				reply: [],
				reviewData: {},
				scores: [],
				contents: [],
				labelStyle: {
					fontSize: '15px'
				}
			};
		},
		onLoad(option) {
			this.isAddon = option.addon == '1'
			uni.setNavigationBarTitle({
				title: this.isAddon ? '追加评价' : '查看评价'
			});
			let pageUrl = location.href;
			let urlList = pageUrl.split('/');
			this.imgUrl = 'http://' + urlList[2];
			// this.imgUrl = 'http://' + "jzny2.cloudcqc.com:46780";
			this.loadData(option.sn)
		},
		// #ifndef MP
		onNavigationBarButtonTap(e) {
			const index = e.index;
			if (index === 0) {
				this.publishReview()
			}
		},
		// #endif
		methods: {
			returnIndex() {
				goback()
			},
			loadData(sn) {
				const params = {
					sn: sn
				}
				getBySn(params).then(res => {

					if (res.code === 0) {
						this.orderData = res.data;
						seeReview({
							orderSn: sn
						}).then(response => {
							if (response.code === 0) {
								const temp = response.data.page[0];
								const reply = response.data.reply
								this.rates.push(temp.serviceScore)
								this.rates.push(temp.qualityScore)
								this.rates.push(temp.priceScore)
								this.rates.push(temp.logisticsScore)
								this.reply = JSON.parse(JSON.stringify(response.data.reply));
								const list = temp.items.filter(i => !i.parentId)
								const addonList = temp.items.filter(i => i.parentId)
								list.forEach(item => {
									const t = addonList.filter(a => a.parentId == item.id)
									t.forEach(tr => {
										if (reply[tr.id]) {
											this.$set(tr, 'replyList', reply[tr.id])
										}
									})
									this.$set(item, 'addonList', t)
									if (this.isAddon) {
										this.$set(item, 'contexts', '')
									}
									if (reply[item.id])
										this.$set(item, 'replyList', reply[item.id])
								})
								this.reviewData = {
									...temp,
									items: list
								}
							}
						})
					}
				})
			},
			// 图片预览功能
			doPreview(imgList, type, index) {
				let urls = []
				if (type === 0) {
					imgList.forEach(item => {
						urls.push(item.fileUrl)
					})
				} else {
					urls = imgList
				}
				uni.previewImage({
					urls: urls,
					loop: true,
					current: index
				});
			},
			onChange(e, type, index) {
				if (type === 's') {
					this.rate = e.index
					this.rates[index] = e.index;
				} else {
					this.scores[index] = e.index;
				}
			},
			publishReview() {
				let reviewItems = []
				let flag = false
				// 追加评价
				for (let index = 0; index < this.reviewData.items.length; index++) {
					const item = this.reviewData.items[index]
					if (!item.contexts) {
						this.$util.msg('商品追加评价不能为空!')
						flag = true
						break;
					}
					let reviewItem = {};
					reviewItem.parentId = item.id;
					reviewItem.orderItemId = item.orderItemId;
					reviewItem.productId = item.productId;
					reviewItem.bigProductId = item.orderItem.bigProductId;
					reviewItem.productName = item.productName;
					reviewItem.productThumbnail = item.productThumbnail;
					reviewItem.productPrice = item.productPrice;
					reviewItem.score = item.score;
					// reviewItem.files = item.files
					reviewItem.content = item.contexts
					reviewItems.push(reviewItem);
				}
				console.log(reviewItems)
				if (flag) return
				addAddonReviewItem(reviewItems).then(res => {
					if (res.code === 0) {
						this.$util.msg('追加评价成功')
						setTimeout(() => {
							getApp().globalData.evaluateRefresh = 1
							uni.navigateBack()
						}, 700)
					} else {
						this.$util.msg(res.message)
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	page,
	.container {
		background: #f5f5f5;
	}

	.supplier-section {
		margin: 20rpx;
		padding: 20rpx;
		background: #fff;
		border-radius: 20rpx;
		display: flex;
		flex-direction: column;

		.name-area {
			display: flex;
			align-items: center;
			margin-bottom: 30rpx;

			.logo {
				width: 34rpx;
				height: 34rpx;
				margin-right: 10rpx;
			}

			.s-name {
				font-size: 15px;
			}
		}

		.s-rate {
			display: flex;
			align-items: center;
			margin-bottom: 20rpx;

			.tit {
				font-size: 14px;
				// font-weight: bold;
			}

			.rate {
				margin-left: 20rpx;
			}
		}
	}

	.goods-section {
		margin: 20rpx;
		display: flex;
		flex-direction: column;

		.good-item {
			padding: 20rpx;
			background: #fff;
			border-radius: 20rpx;
			display: flex;
			flex-direction: column;
			margin-bottom: 20rpx;

			.good-info {
				display: flex;

				.image-wrapper {
					width: 80px;
					height: 80px;
					flex-shrink: 0;
					background-color: #fff;
					border-radius: 8upx;
				}

				.item-right {
					display: flex;
					flex-direction: column;
					flex: 1;
					overflow: hidden;
					justify-content: space-between;
					position: relative;
					padding: 12rpx 20upx;

					.title {
						width: 100%;
						color: $font-color-dark;
						font-size: $font-base;
						overflow: hidden;
						text-overflow: ellipsis;
						word-break: break-all;
					}

					/*多行文本溢出省略号*/
					.more-t {
						overflow: hidden; // 超出的文本隐藏
						text-overflow: ellipsis; // 溢出用省略号显示
						display: -webkit-box; // 将对象作为弹性伸缩盒子模型显示。
						-webkit-line-clamp: 2; // 这个属性不是css的规范属性，需要组合上面两个属性，表示显示的行数。
						-webkit-box-orient: vertical; // 从上到下垂直排列子元素（设置伸缩盒子的子元素排列方式）
						transition: all linear 0.2s;
					}

					.price {
						font-size: $font-base + 2upx;
						color: $uni-color-primary;
						height: 40upx;
						line-height: 40upx;
					}

				}
			}

			.g-rate {
				display: flex;
				align-items: center;
				margin-top: 20rpx;

				.tit {
					font-size: 14px;
				}

				.rate {
					margin-left: 20rpx;
				}
			}

			.content {
				margin-top: 20rpx;
				background: #fcfcfc;
				height: fit-content;
				min-height: 140rpx;
				width: 100%;
				// max-height: 200rpx;
				border-radius: 10rpx;
				padding: 16rpx;
				color: #303133;
				font-size: 14px;
				border: 1px solid #d9d9d9;
			}

			.ph {
				font-size: 14px;
				color: #666;
			}
		}
	}
</style>
